package com.ywb;


import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

/**
 * @Author ywb
 */
public class EsBatchClient {
    public static void main(String[] args)  throws  Exception{
        RestHighLevelClient ESClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

        BulkRequest bulk = new BulkRequest();
        bulk.add(new IndexRequest("java-demo").id("0001").source(XContentType.JSON, "name", "zhagnsan", "age", 20));
        bulk.add(new IndexRequest("java-demo").id("0002").source(XContentType.JSON, "name", "lisi", "age", 20));
        bulk.add(new IndexRequest("java-demo").id("0003").source(XContentType.JSON, "name", "wangwu", "age", 20));
        bulk.add(new DeleteRequest("java-demo").id("0001"));

        BulkResponse result = ESClient.bulk(bulk, RequestOptions.DEFAULT);
        System.out.println(result.getTook());
        ESClient.close();
    }
}
